#!/usr/bin/python
# -*- coding: UTF-8 -*-
import subprocess
import time
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility


class SNMP_SYSTEM_TIME_001(Case):
    """
            Name:
                SNMP_SYSTEM_TIME_001
            Label:
                系统时间
            PreCondition:
                iBMC正常启动,snmp v1 v3 v3服务器启动
            TestStep:
                1) 使用snmpwalk 工具进行snmp v1 v2 v3各版本查询系统时间，预期结果A
                2) snmp v1、v2、v3获取系统时间进行结果比对，预期结果B
            ExpectedResult:
                A) 各协议版本获取系统时间成功
                B) snmp v1、v2、v3获取系统时间一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:
                因执行程序有时差，获取的时间可能存在极小误差
        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DST)
        self.os_host = self.device_dut.get_host(host_type=HostType.HostOS)
        self.os_ip = self.os_host.local_ip

        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_password = self.bmc_host.password

        self.snmp_read = "Admin#234"
        self.snmp_v3_password = "Admin#234"
        self.snmp_number = "1.3.6.1.4.1.2011.2.235.1.1.1.3.0"

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: snmpwalk snmp v1 命令查询系统时间")
        # snmp v1
        cmd_v1 = f"snmpwalk -v 1 -c '{self.snmp_read}' {self.bmc_ip} {self.snmp_number}"
        result_v1 = self.os_host.run({"command": [cmd_v1], "timeout": 10})
        rc_v1 = result_v1['rc']
        if rc_v1 != 0:
            stderr_v1 = result_v1['stderr']
            self.assertIsNotNone(None, f"snmpwalk command err: {stderr_v1}")
        stdout_v1 = result_v1['stdout']
        self.logger.info(f"retrurn code: {rc_v1}, stdout: {stdout_v1}")

        # snmp v2
        self.log_step("Step2: snmpwalk snmp v2 命令查询系统时间")
        cmd_v2 = f"snmpwalk -v 2c -c '{self.snmp_read}' {self.bmc_ip} {self.snmp_number}"
        result_v2 = self.os_host.run({"command": [cmd_v2], "timeout": 10})
        rc_v2 = result_v2['rc']
        if rc_v2 != 0:
            stderr_v2 = result_v2['stderr']
            self.assertIsNotNone(None, f"snmpwalk command err: {stderr_v2}")
        stdout_v2 = result_v2['stdout']
        self.logger.info(f"retrurn code: {rc_v2}, stdout: {stdout_v2}")

        # snmp v3
        self.log_step("Step3: snmpwalk snmp v3 命令查询系统时间")
        cmd_v3 = f"snmpwalk -v 3 -u {self.bmc_user} -l authPriv -a sha256 -A {self.bmc_password} -x aes -X {self.snmp_v3_password} {self.bmc_ip} {self.snmp_number}"
        result_v3 = self.os_host.run({"command": [cmd_v3], "timeout": 10})
        rc_v3 = result_v3['rc']
        if rc_v3 != 0:
            stderr_v3 = result_v3['stderr']
            self.assertIsNotNone(None, f"snmpwalk command err: {stderr_v3}")
        stdout_v3 = result_v3['stdout']
        self.logger.info(f"retrurn code: {rc_v3}, stdout: {stdout_v3}")

        self.log_step("Step4: 比对snmp v1 v2 v3三者查询的系统时间是否一致（秒级可能会存在极小误差，属于正常现象）")
        self.logger.info(f"SNMP查询的系统时间：\n{stdout_v1}\n{stdout_v2}\n{stdout_v3}")
        if stdout_v1[:-2] == stdout_v2[:-2] == stdout_v3[:-2]:
            self.logger.info("比对snmp v1 v2 v3三者查询的系统时间一致")
        else:
            self.assertIsNotNone(None, f"比对snmp v1 v2 v3三者查询的系统时间不一致\n{stdout_v1[:-2]}\n{stdout_v2[:-2]}\n{stdout_v3[:-2]}")

    # 测试结束后恢复环境
    def post_test_case(self):
         pass

